using System;
using System.Collections;
using System.Collections.Generic;
using RMA.Rhino;

namespace ResponsiveRhino
{
    ///<summary>
    /// Every Rhino.NET Plug-In must have one and only one MRhinoPlugIn derived
    /// class. DO NOT create an instance of this class. It is the responsibility
    /// of Rhino.NET to create an instance of this class and register it with Rhino.
    ///</summary>
    public class ResponsiveRhinoPlugIn : RMA.Rhino.MRhinoUtilityPlugIn
    {
        //private ResponsiveRhinoPlugInMenu RhinoIOMenu; // retrieve the menu of Rhino
        private RhinoIOData rIOData;        

        ///<summary>
        /// Rhino tracks plug-ins by their unique ID. Every plug-in must have a unique id.
        /// The Guid created by the project wizard is unique. You can create more Guids using
        /// the "Create Guid" tool in the Tools menu.
        /// </summary>
        /// <returns>The id for this plug-in</returns>
        public override System.Guid PlugInID()
        {
            return new System.Guid("{41128288-f961-4057-b0d6-43ddbfbba8be}");
        }

        /// <returns>Plug-In name as displayed in the plug-in manager dialog</returns>
        public override string PlugInName()
        {
            return "ResponsiveRhino";
        }

        ///<returns>Version information for this plug-in</returns>
        public override string PlugInVersion()
        {
            return "1.0.0.0";
        }

        ///<summary>
        /// Called after the plug-in is loaded and the constructor has been run.
        /// This is a good place to perform any significant initialization,
        /// license checking, and so on.  This function must return 1 for
        /// the plug-in to continue to load.
        ///</summary>
        ///<returns>
        ///  1 = initialization succeeded, let the plug-in load
        ///  0 = unable to initialize, don't load plug-in and display an error dialog
        /// -1 = unable to initialize, don't load plug-in and do not display an error
        ///      dialog. Note: OnUnloadPlugIn will not be called
        ///</returns>
        public override int OnLoadPlugIn()
        {
            rIOData = new RhinoIOData();
            //RhinoIOMenu = new ResponsiveRhinoPlugInMenu();
            //InsertPlugInMenuToRhinoMenu(RhinoIOMenu);

            return 1;
        }

        ///<summary>
        /// Called when the plug-in is about to be unloaded.  After this
        /// function is called, the plug-in will be disposed.
        ///</summary>
        public override void OnUnloadPlugIn()
        {
            // TODO: Add plug-in cleanup code here.
        }

        public override object GetPlugInObjectInterface(ref Guid iid)
        {
            //return base.GetPlugInObjectInterface(ref iid);
            //return new RhinoArduino();
            return rIOData;
        }

    }
    public class ResponsiveRhinoPlugInMenu : RMA.UI.MRhinoPlugInMenu
    {
        public ResponsiveRhinoPlugInMenu()
            : base("ResponsiveRhino")
        {
            //just build the entire menu in the constructor
            AppendString("Run Rhino I/O", new EventHandler(this.HandleRunRhinoIO));
            AppendSeparator();
        }

        private void HandleRunRhinoIO(object sender, EventArgs e)
        {
            RhUtil.RhinoApp().RunScript("RhinoIO");
        }

    }

    [System.Runtime.InteropServices.ComVisible(true)]
    public class RhinoIOData
    {
        //a list of subscribed topic as key and the current double value of the topic
        //private Dictionary<string, double> topicList = new Dictionary<string, double>();
        private Rhino_IO_UI ui;

        public RhinoIOData()
        {
            ui = new Rhino_IO_UI();
            ui.Show();
        }

        public Dictionary<string, double> TopicList
        {
            get
            {
                return ui.topicList;
            }
        }

        public double GetLight()
        {
            return ui.topicList["ARD|LIGHT"];
        }

        public double GetSound()
        {
            return ui.topicList["ARD|SOUND"];
        }

        public double GetTopicValue(string topic)
        {
            return ui.topicList[topic];
        }
    }
}
